package com.furioussnails.sourcecodeanalysis.parsing;

public class GenericTokenParser {

    /**
     * 开始标记
     */
    private final String openToken;

    /**
     * 结束标记
     */
    private final String closeToken;

    /**
     * 标记处理器
     */
    private final TokenHandler handler;

    public GenericTokenParser(String openToken, String closeToken, TokenHandler handler) {
        this.openToken = openToken;
        this.closeToken = closeToken;
        this.handler = handler;
    }

    /**
     * 解析#{}和${} 该方法主要实现了配置文件,脚本等片段中占位符的解析,处理工作,并返回最终需要的数据  
     * select * from user where id = ?  这种形式
     * 解析工作由该方法完成,处理工作是Handler的handlerToken()方法来实现
     * @param text
     * @return
     */
    public String parse(String text) {

        // 验证参数
        if (text == null || text.isEmpty()) {
            return "";
        }

        // 下面继续验证是否包含占位符,如果不包含,直接原样返回.
        int start = text.indexOf(openToken);
        if (start == -1) {
            return text;
        }

        // 把text转为char[]字符数组,定义默认偏移量为0, 最终返回的字符串builder
        // text变量中占位符对应的变量名为expression. 判断start是否大于-1(即text中是否包含占位符)
        char[] src = text.toCharArray();
        int offset = 0;
        final StringBuilder builder = new StringBuilder();
        StringBuilder expression = null;
        while (start > -1) {
            // 判断占位符前面是否有转义字符\
            if (start > 0 && src[start - 1] == '\\') {
                builder.append(src, offset, start - offset - 1).append(openToken);
                offset = start + openToken.length();
            } else {
                //重置expression变量,避免空指针异常或者老数据干扰
                if (expression == null) {
                    expression = new StringBuilder();
                } else {
                    expression.setLength(0);
                }
                builder.append(src, offset, start - offset);
                offset = start + openToken.length();
                int end = text.indexOf(closeToken,offset);
                while (end > -1) {
                    if (end > offset && src[end - 1] == '\\') {
                        expression.append(src, offset, end - offset - 1).append(closeToken);
                        offset = end + closeToken.length();
                        end = text.indexOf(closeToken, offset);
                    } else {
                        expression.append(src, offset, end - offset);
                        break;
                    }
                }
                if (end == -1) {
                    // 找不到关闭字符(没有对应"}")
                    builder.append(src, start, src.length - start);
                    offset = src.length;
                } else {
                    // 首先根据参数的key (expression)进行参数处理,返回?占位符
                    builder.append(handler.handleToken(expression.toString()));
                    offset = end + closeToken.length();
                }
            }
            start = text.indexOf(openToken, offset);
        }
        if (offset < src.length) {
            builder.append(src, offset, src.length - offset);
        }
        return builder.toString();
    }
}
